package com.hunan.api.catv.mq.consumer;

import com.hunan.api.catv.entity.enums.FilesStateEnum;
import com.hunan.api.catv.entity.vo.ActivityVideoVo;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.activity.ActivityVideoBiz;
import com.hunan.api.catv.service.biz.toupiao.TouPiaoBiz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class AddTicketConsumer {

    public static Logger log = LoggerFactory.getLogger(AddTicketConsumer.class);

    @Autowired
    ActivityVideoBiz activityVideoBiz;

    @Autowired
    TouPiaoBiz touPiaoBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.add_ticket_queue, durable = "true"), exchange = @Exchange(value = MqConstants.add_ticket_exchange, durable = "true", type = "topic"), key = MqConstants.add_ticket_key))
    public void tasks(Integer id) {
        log.info("[AddTicketToUser:{}]", id);
        try {
            ActivityVideoVo activityVideoVo = activityVideoBiz.getById(id);
            if (!ObjectUtils.isEmpty(activityVideoVo)) {
                if (activityVideoVo.getState().equals(FilesStateEnum.normal.getCode())) {
                    touPiaoBiz.addTicketToUser(activityVideoVo.getMemberId(), 1);
                }
            }
        } catch (Exception e) {
            log.error("[AddTicketToUser Error:{}]", e);
        }
    }
}
